/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.perlak.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author mateusz
 */
public class DateUtil {

  /**
   * Datetime format.
   */
  public static final String DATE_FORMAT = "MM-dd-yyyy hh:mm:ss.SSS";
  /**
   * Datetime format.
   */
  public static final String SHORT_DATE_FORMAT = "yyyy-MM-dd";

  /**
   * Private constructor.
   */
  private DateUtil() {
  }
  /**
   * Logger.
   */
  private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

  /**
   * Convert calendar date to string.
   * @param cal calendar date to convert.
   * @return date string.
   */
  public static String calendarToString(final Calendar cal) {
    if (cal == null) {
      return null;
    }
    DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
    return formatter.format(cal.getTime());
  }

  /**
   * Convert date to string.
   * @param date date to convert.
   * @return date string.
   */
  public static String dateToString(final Date date) {
    if (date == null) {
      return null;
    }
    DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
    return formatter.format(date);
  }

  /**
   * Convert string to calendar date.
   * @param dateStr string to convert.
   * @return date.
   */
  public static Calendar stringToCalendar(final String dateStr) {
    Calendar cal = null;
    try {
      if (StringUtils.isBlank(dateStr)) {
        return null;
      }
      DateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
      Date date = sdf.parse(dateStr);
      cal = Calendar.getInstance();
      cal.setTime(date);
    } catch (ParseException ex) {
      logger.error("Exception while parsing date string(" + dateStr + ")to calendar :" + ex.getMessage());
    }
    return cal;
  }

  /**
   * Convert string to date.
   * @param dateStr string to convert.
   * @return date.
   */
  public static Date stringToDate(final String dateStr) {
    Date date = null;
    try {
      if (StringUtils.isBlank(dateStr)) {
        return null;
      }
      DateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
      date = sdf.parse(dateStr);
    } catch (ParseException ex) {
      logger.error("Exception while parsing date string(" + dateStr + ")to date :" + ex.getMessage());
    }
    return date;
  }

  /**
   * Convert string to java date excluding time.
   * @param dateStr string to convert.
   * @return date.
   */
  public static Date stringToShortDate(final String dateStr) {
    Date date = null;
    try {
      if (StringUtils.isBlank(dateStr)) {
        return null;
      }
      DateFormat sdf = new SimpleDateFormat(SHORT_DATE_FORMAT);
      date = sdf.parse(dateStr);
    } catch (ParseException ex) {
      logger.error("Exception while parsing date string(" + dateStr + ")to date :" + ex.getMessage());
    }
    return date;
  }
}